<?php declare(strict_types=1);
/**
 * Created by IntelliJ IDEA
 * Author: 张伯发
 * Date  : 2019/11/19
 * Time  : 11:02
 */

namespace App\Controller;


use App\Dto\Request\Members\MembersLoginRequestDTO;
use App\Dto\Request\Members\MembersLogoutRequestDTO;
use App\Dto\Request\Members\MembersRegisterRequestDTO;
use App\Dto\Request\Members\MembersVerifyCodeRequestDTO;
use App\Service\Members\MembersLoginRegisterLogoutService;
use App\Service\VerifyCodeService;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Http\Message\Request;
use Swoft\Validator\Annotation\Mapping\Validate;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Db\Exception\DbException;
use \ReflectionException;
use Swoft\Validator\Annotation\Mapping\ValidateType;
use Xiaoproject\Common\Json\Json;
use Xiaoproject\Common\Language\L;

/**
 * Class MembersController
 * @author 张伯发 2019/11/19 11:02
 * @Controller(prefix="members")
 */
class MembersController
{
    /**
     * @Inject()
     * @var MembersLoginRegisterLogoutService
     */
    protected $membersLoginRegisterLogoutService;

    /**
     * @Inject()
     * @var VerifyCodeService
     */
    protected $verifyCodeService;

    /**
     * 注册会员
     *
     * @param Request $request
     * @return array
     * @throws ContainerException
     * @throws DbException
     * @throws ReflectionException
     * @author 张伯发 2019/11/19 11:02
     * @RequestMapping(method={RequestMethod::POST}, route="register")
     * @Validate(validator="MembersRegisterValidator")
     */
    public function register(Request $request)
    {
        $body = $request->getRawBody();
        $result = jsonMapper($body,  MembersRegisterRequestDTO::class);
        $result = $this->membersLoginRegisterLogoutService->register($result);
        vdump($result);

        $logMessage = $this->membersLoginRegisterLogoutService->getMessage();
        if (empty($result)) return Json::E($logMessage);
        return Json::S($result);
    }

    /**
     * @param Request $request
     * @return array
     * @throws DbException
     * @author 张伯发 2019/11/19 18:07
     * @RequestMapping(route="login", method={RequestMethod::POST})
     * @Validate(validator="MembersLoginValidator")
     */
    public function login(Request $request)
    {
        $result = jsonMapper($request->getRawBody(), MembersLoginRequestDTO::class);

        $login = $this->membersLoginRegisterLogoutService->login($result);

        if (empty($login)) {
            return Json::E($this->membersLoginRegisterLogoutService->getMessage());
        }
        return Json::S($login);
    }


    /**
     * @param Request $request
     * @return array
     * @author 张伯发 2019/11/23 10:48
     * @RequestMapping(route="logout", method={RequestMethod::POST})
     * @Validate(validator="MembersTokenValidator")
     */
    public function logout(Request $request)
    {
        $requestJsonObj = jsonMapper($request->getRawBody(), MembersLogoutRequestDTO::class);

        $result = $this->membersLoginRegisterLogoutService->logout($requestJsonObj);
        if ($result === false) {
            return Json::E($this->membersLoginRegisterLogoutService->getMessage());
        }
        return Json::SMessage();
    }


    /**
     * 获取注册验证码
     * @param Request $request
     * @return array
     * @author 张伯发 2019/11/21 11:50
     * @RequestMapping(route="getVerifyCode", method={RequestMethod::POST})
     * @Validate(validator="MembersVerifyCodeValidator", type=ValidateType::BODY)
     */
    public function getVerifyCode(Request $request)
    {
        $result = jsonMapper($request->getRawBody(), MembersVerifyCodeRequestDTO::class);
        $set = $this->verifyCodeService->setMembersRegisterCode($result, 6);
        if ($set === false) {
            return Json::E(L::get('members.register_verify_code_send_failed'));
        }
        return Json::SMessage(L::get('members.register_verify_code_send_success'));
    }
}
